<?php

namespace App\Admin\Actions\Audit;

use App\Library\Logic\OperateLogLogic;
use App\Models\AuctionModel;
use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Exception;
use Illuminate\Http\Request;

class AuditAuctionAction extends RowAction
{
    public $name = '审核';

    /**
     * random
     * @param AuctionModel $model
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function handle(AuctionModel $model, Request $request): Response
    {
        if ($model->audit_status !== 0) return $this->response()->error('已审核！')->refresh();
        if ((int)$request->audit_status === 2) {
            $model->fail_reason = $request->reason ?? null;
            $model->status = 0;
        } else $model->ok_reason = $request->reason ?? null;
        $model->audit_at = now();
        $model->audit_status = $request->audit_status;
        //$model->is_start = 1;
        $model->save();
        if ((int)$model->audit_status === 1) mq('auctionStart', $model->serial_number, $model->start_at);
        OperateLogLogic::add(Admin::user()->user_id, 'admin.auction.audit');
        if ($model->one_push_status === 1) mq('oneKeyPush', $model->serial_number);
        return $this->response()->success('审核成功！')->refresh();
    }

    public function form()
    {
        $this->select('audit_status', '审核')->options([1 => '同意', 2 => '拒绝'])->default(2)->required();
        $this->text('reason', '原因')->attribute('maxlength', 255);
    }
}
